package negocio.bd;

import base.Factura;
import java.sql.SQLException;
import java.util.ArrayList;
import utiles.ConexionBD;
import utiles.Logger;

/**
 *
 * @author Efren
 */
public class BDFacturas {
    public static final String idFactura = "idFactura";
    public static final String subTotal = "subTotal";
    public static final String descuento = "descuento";
    public static final String total = "total";
    public static final int buscarPorId = 1;
    public static final int buscarPorTotal = 2;
    public static final int ASC = 1;
    public static final int DESC = 2;
    
    public static Factura[]  buscar(int campoBusqueda, String busqueda, int orden, boolean exacta){
        java.sql.Connection db = new ConexionBD().getConnection();
        ArrayList<Factura> facturas = new  ArrayList<>(0);
        String query = "SELECT * FROM `gestionTienda`.`Factura` ";
        switch (campoBusqueda){
            case buscarPorTotal:
                query += "WHERE " + total + " like " + busqueda +"% ORDER BY " + total;
                break;
            default:
                query += "WHERE " + idFactura + " like " + busqueda + "% ORDER BY " + idFactura;
        }
        //Si la busqueda a de ser exacta quitar like y % de la consulta
        if (exacta){
            query = query.replaceAll("like", "=");
            query = query.replace("%", "");
        }
        if (orden == DESC){
            query += " DESC";
        } else {
            query += " ASC";
        }
        try {
            java.sql.Statement statement = db.createStatement();
            java.sql.ResultSet res = statement.executeQuery(query);
            while (res.next()){
                Factura temp = new Factura();
                temp.setIdFactura(res.getLong(idFactura));
                temp.setSubTotal(res.getDouble(subTotal));
                temp.setDescuento(res.getFloat(descuento));
                temp.setTotal(res.getDouble(total));
                facturas.add(temp);
            }
            return facturas.toArray(new Factura[0]);
        } catch (SQLException ex){
            Logger.printError(ex.getMessage());
        }
        return null;
    }
    public static Factura obtener(long idFactura){
        java.sql.Connection db = new ConexionBD().getConnection();
        try {
            java.sql.Statement select = db.createStatement();
            java.sql.ResultSet res = select.executeQuery("SELECT * FROM `gestionTienda`.`Factura` WHERE " + BDFacturas.idFactura + " = " + idFactura);
            while (res.next()){
                Factura temp = new Factura();
                temp.setIdFactura(res.getLong(BDFacturas.idFactura));
                temp.setSubTotal(res.getDouble(subTotal));
                temp.setDescuento(res.getFloat(descuento));
                temp.setTotal(res.getDouble(total));
                return temp;
            }
        }catch (SQLException ex){
            Logger.printError(ex.getMessage());
        }return null;
    }
    public static boolean existe(long idFactura){
        java.sql.Connection db = new ConexionBD().getConnection();
        try {
            java.sql.Statement select = db.createStatement();
            java.sql.ResultSet res = select.executeQuery("SELECT * FROM `gestionTienda`.`Factura` WHERE " + BDFacturas.idFactura + " = " + idFactura);
            if (res.next()){
                return true;
            }
        } catch (SQLException ex){
            Logger.printError(ex.getMessage());
        }return false;
    }
    public static Factura[] obtenerTodos(){
        java.sql.Connection db = new ConexionBD().getConnection();
        ArrayList<Factura> facturas = new ArrayList<>(0);
        try {
            java.sql.Statement select = db.createStatement();
            java.sql.ResultSet res = select.executeQuery("SELECT * FROM `gestionTienda`.`Factura`");
            while (res.next()){
                Factura temp = new Factura();
                temp.setIdFactura(res.getLong(idFactura));
                temp.setSubTotal(res.getDouble(subTotal));
                temp.setDescuento(res.getFloat(descuento));
                temp.setTotal(res.getDouble(total));
                facturas.add(temp);
            }
            return facturas.toArray(new Factura[0]);
        } catch (SQLException ex){
            Logger.printError(ex.getMessage());
        }return null;
    }
    public static boolean insertar(Factura factura){
        if (!existe(factura.getIdFactura())){
            java.sql.Connection db = new ConexionBD().getConnection();
            try {
                java.sql.PreparedStatement insert = db.prepareStatement("INSERT INTO `gestionTienda`. `Factura` VALUES (?,?,?,?)");
                insert.setLong(1, factura.getIdFactura());
                insert.setDouble(2, factura.getSubTotal());
                insert.setFloat(3, factura.getDescuento());
                insert.setDouble(4, factura.getTotal());
                insert.executeUpdate();
                return true;
            } catch (SQLException ex){
                Logger.printError(ex.getMessage());
            }
        }return false;
    }
    public static boolean actualizar(Factura factura){
        if (existe(factura.getIdFactura())){
            java.sql.Connection db = new ConexionBD().getConnection();
            try {
                java.sql.PreparedStatement update = db.prepareStatement("UPDATE `gestionTienda`.`Factura` SET " + subTotal + " = ? " + descuento + " = ? " + total +" = ? WHERE " + BDFacturas.idFactura + " = ?");
                update.setDouble(1, factura.getSubTotal());
                update.setFloat(2, factura.getDescuento());
                update.setDouble(3, factura.getTotal());
                update.setLong(4, factura.getIdFactura());
                if (update.executeUpdate()==1){
                    return true;
                }
            } catch (SQLException ex){
                Logger.printError(ex.getMessage());
            }
        }return false;
    }
    
}
